<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\controller;

/**
 * 文章控制器
 */
class Article extends AdminBase
{
    //极光推送
    public function jpush(){
        $article=db('article');
        $param=$this->param;
        $info=$article->where("id",$param['id'])->find();
        import("Jpush.autoload");
        if($param['type']==1){
            $app_key='e5e770b8982167b38d4728a3';
            $master_secret='522afaa3b60b7544a6458b84';
        }else{
            $app_key='eb37b66727665f5075aa5545';
            $master_secret='48f0f8356f837970190af5f6';
        }
        $u='http://'.$_SERVER['SERVER_NAME'].'/index.php/index/details/id/'.$param['id'];
        $client = new \JPush\Client($app_key, $master_secret);
        $pusher = $client->push();
        $pusher->setPlatform('all');
        $pusher->addAllAudience();
        //$pusher->addAlias('10006');
        //$pusher->setNotificationAlert($info['name']);
        $pusher->iosNotification([
            'title'=>$info['name'],
            'subtitle'=>$info['describe']
        ],[
            'sound' => 'sound',
            'badge' => '+1',
            'extras' => [
                'url' => $u
            ]
        ]);
        $pusher->options([
            'apns_production' => true
        ]);
        $pusher->androidNotification($info['describe'],[
            'title'=>$info['name'],
            'extras' => [
                'url' => $u
            ]
        ]);
        try {
            $pusher->send();
        } catch (\JPush\Exceptions\JPushException $e) {
            // try something else here
            print $e;exit;
        }
        $re=$article->where("id",$param['id'])->update(array('ts'=>1));
        $re && action_log('APP推送', 'APP推送，article_id：' . $param['id']);
        echo json_encode(array('error'=>0,'html'=>'推送成功！'));
        exit;
    }
    /**
     * 文章列表
     */
    public function articleList()
    {
        
        $where = $this->logicArticle->getWhere($this->param);
        
        $this->assign('list', $this->logicArticle->getArticleList($where, 'a.*,m.nickname,c.name as category_name', 'a.create_time desc'));
        
        return $this->fetch('article_list');
    }
    
    /**
     * 文章添加
     */
    public function articleAdd()
    {
        
        $this->articleCommon();
        
        return $this->fetch('article_edit');
    }
    
    /**
     * 文章编辑
     */
    public function articleEdit()
    {
        
        $this->articleCommon();
        
        $info = $this->logicArticle->getArticleInfo(['a.id' => $this->param['id']], 'a.*,m.nickname,c.name as category_name');
        
        !empty($info) && $info['img_ids_array'] = str2arr($info['img_ids']);
        
        $this->assign('info', $info);
        
        return $this->fetch('article_edit');
    }
    
    /**
     * 文章添加与编辑通用方法
     */
    public function articleCommon()
    {
        
        IS_POST && $this->jump($this->logicArticle->articleEdit($this->param));
        
        $this->assign('article_category_list', $this->logicArticle->getArticleCategoryList([], 'id,name', '', false));
    }
    
    /**
     * 文章分类添加
     */
    public function articleCategoryAdd()
    {
        
        IS_POST && $this->jump($this->logicArticle->articleCategoryEdit($this->param));
        
        return $this->fetch('article_category_edit');
    }
    
    /**
     * 文章分类编辑
     */
    public function articleCategoryEdit()
    {
        
        IS_POST && $this->jump($this->logicArticle->articleCategoryEdit($this->param));
        
        $info = $this->logicArticle->getArticleCategoryInfo(['id' => $this->param['id']]);
        
        $this->assign('info', $info);
        
        return $this->fetch('article_category_edit');
    }
    
    /**
     * 文章分类列表
     */
    public function articleCategoryList()
    {
        
        $this->assign('list', $this->logicArticle->getArticleCategoryList());
       
        return $this->fetch('article_category_list');
    }
    
    /**
     * 文章分类删除
     */
    public function articleCategoryDel($id = 0)
    {
        
        $this->jump($this->logicArticle->articleCategoryDel(['id' => $id]));
    }
    
    /**
     * 数据状态设置
     */
    public function setStatus()
    {
        
        $this->jump($this->logicAdminBase->setStatus('Article', $this->param));
    }
    /**
     * 排序
     */
    public function setSort()
    {

        $this->jump($this->logicAdminBase->setSort('ArticleCategory', $this->param));
    }
}
